<template>
  <div class="uni-tabs-content-croll">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'" class="uni-scroll">
      <el-row>
        <el-col :lg="12" :md="14" :sm="16" :xs="24">
          <el-form-item prop="appKey" :label="$t('dingTalk.appKey')">
            <el-input v-model="dataForm.appKey" :placeholder="$t('dingTalk.appKey')"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :lg="12" :md="14" :sm="16" :xs="24">
          <el-form-item prop="appSecret" :label="$t('dingTalk.appSecret')">
            <el-input v-model="dataForm.appSecret" :placeholder="$t('dingTalk.appsecret')"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :lg="12" :md="14" :sm="16" :xs="24">
          <el-form-item prop="agentId" :label="$t('msgDingtalkAccount.agentId')">
            <el-input v-model="dataForm.agentId" :placeholder="$t('msgDingtalkAccount.agentId')"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- <el-col :span="24" style="text-align:center;margin-top: 30px;">
      <el-button size="small" type="primary" @click="dataFormSubmitHandle()">{{ $t('save') }}</el-button>
    </el-col> -->
    <div class="uni-tab-form-bottom">
      <el-button size="small" type="primary" @click="dataFormSubmitHandle()">{{ $t('save') }}</el-button>
    </div>
  </div>
</template>

<script>
import debounce from 'lodash/debounce'
// import mainContent from '@/views/main-content'
// var content = mainContent
export default {
  data () {
    return {
      timestamp: this.$route.params.timestamp,
      loading: false,
      dataForm: {
        appKey: '',
        appSecret: '',
        agentId: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        appKey: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        appSecret: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        agentId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    // this.dataForm.id = this.$route.params.id
    this.init()
  },
  methods: {
    init () {
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.getInfo()
      })
    },
    // 获取信息
    getInfo () {
      this.loading = true
      this.$http.get(`/sys/appConfig/dingTalk`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
        this.loading = false
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http['post']('/sys/appConfig/dingTalk', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style scoped>
</style>
